package com.hilinos.base.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hilinos.base.api.vo.OrgInfoVo;
import com.hilinos.base.app.OrgApp;
import com.hilinos.base.user.entity.pojo.OrgInfoPoJo;
import com.hilinos.common.common.ReturnCode;
import com.hilinos.common.common.handler.NoToken;
import com.hilinos.common.entity.BaseQo;
import com.hilinos.common.util.AssertUtils;
import com.hilinos.common.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 管理机构信息
 */
@RestController
@RequestMapping("org")
public class OrgController {

    @Autowired
    private OrgApp orgApp;

    @PostMapping("create")
    public void create(OrgInfoPoJo orgInfo) {
        orgInfo.setAppid(ServletUtils.getAppid());
        orgApp.create(orgInfo);
    }

    @PostMapping("edit")
    public void edit(OrgInfoPoJo orgInfo) {
        AssertUtils.isNotEmpty(orgInfo.getOrgId(), ReturnCode.NORMAL_PARAM_LOST_ERROR.respMsg("机构id不能为空"));
        orgInfo.setAppid(ServletUtils.getAppid());
        orgApp.edit(orgInfo);
    }

    /**
     * 查询机构列表
     *
     * @param qo
     * @return
     */
    @NoToken
    @PostMapping("pageList")
    public IPage<OrgInfoPoJo> pageList(@RequestBody BaseQo<OrgInfoPoJo, OrgInfoPoJo, OrgInfoPoJo> qo) {
        String appid = ServletUtils.getAppid();
        if (!ObjectUtils.isEmpty(qo.getE())) {
            qo.getE().setAppid(appid);
        }

        if (!ObjectUtils.isEmpty(qo.getL())) {
            qo.getL().setAppid(appid);
        }
        return orgApp.pageList(qo);
    }

    /**
     * 查询机构信息
     *
     * @param orgId
     * @return
     */
    @NoToken
    @PostMapping("detail")
    public OrgInfoPoJo detail(String orgId) {
        return orgApp.detail(orgId, ServletUtils.getAppid());
    }

    /**
     * 查询父机构信息
     *
     * @param orgId
     * @return
     */
    @NoToken
    @PostMapping("parent")
    public OrgInfoPoJo parent(String orgId) {
        return orgApp.parent(orgId, ServletUtils.getAppid());
    }

    /**
     * 查询下一级子机构
     *
     * @param orgId
     * @return
     */
    @NoToken
    @PostMapping("child")
    public List<OrgInfoPoJo> child(String orgId) {
        return orgApp.child(orgId, ServletUtils.getAppid());
    }

    /**
     * 查询所有子机构
     *
     * @param orgId
     * @return
     */
    @NoToken
    @PostMapping("childs")
    public List<OrgInfoVo> childs(String orgId) {
        return orgApp.childs(orgId, ServletUtils.getAppid());
    }

}
